import socket
from setting import *

    
class TcpSocket:
    def __init__(self):
        self.server_addr = (ADDR)
        self.sfd = socket.socket(type=TCP_TYPE)
        self.sfd.settimeout(.3)
        self.sfd.connect(self.server_addr)

    def send_to_server(self, msg: str):
        send_msg = msg.encode()
        self.sfd.send(send_msg)

    def recv_from_server(self, recv_size=1024):
        recv_msg = ''
        while 1:
            try:
                msg = self.sfd.recv(recv_size).decode()
            except socket.timeout:
                print('没有更多的信息了')
                break
            recv_msg += msg
            print('msg:', recv_msg)
        return recv_msg


class UdpScoket(TcpSocket):
    def __init__(self):
        super().__init__()

    def recv_from_server(self, recv_size=1024):
        self.msg, self.u_addr = self.sfd.recvfrom(recv_size)
        return self.msg.decode()

    def send_to_server(self, msg:str):
        send_msg = msg.encode()
        self.sfd.sendto(send_msg, self.u_addr)